import time

import aircv as ac

from common.tools import ToolsUtil


class FindImg:

    # 读取图片
    def img_read(self, img_path):
        return ac.imread(img_path)

    # 对比图片
    def get_confidence(self, full_img_path, part_img_path):
        img_full = self.img_read(full_img_path)
        img_part = self.img_read(part_img_path)

        result = ac.find_template(img_full, img_part)
        print(result)

        return result['confidence']

    # 截取完整图片对比部分图片的信心值
    def cap_full_compare_part(self, driver, full_img_name, part_img_name):
        full_img_name = ToolsUtil().ger_project_path() + ToolsUtil.build_path("img", "full_img", full_img_name)
        part_img_name = ToolsUtil().ger_project_path() + ToolsUtil.build_path("img", "part_img", part_img_name)
        driver.get_screenshot_as_file(full_img_name)
        time.sleep(3)
        confidence = FindImg().get_confidence(full_img_name, part_img_name)
        return confidence


if __name__ == '__main__':
    full_img = ToolsUtil.ger_project_path() + ToolsUtil.build_path("img", "full_img", "dangdang_home_page.png")
    part_img = ToolsUtil.ger_project_path() + ToolsUtil.build_path("img", "part_img", "dangdang_home_book_icon.png")
    print(FindImg().get_confidence(full_img, part_img))
